package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.ProfilePictureForm;

@Component
public class ProfilePictureFormValidator implements Validator {
	private static final long MAX_PHOTO_SIZE = 4194304; // 4MB
		
	@Override
	public boolean supports(Class<?> clazz) {
		return ProfilePictureForm.class.isAssignableFrom(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		ProfilePictureForm form = (ProfilePictureForm) target;
		
		if (form.getPhoto().getSize() > MAX_PHOTO_SIZE) {
			errors.rejectValue("photo", "size");
		}
	}
}
